package alarm.ruiqi.com.alarm_pro.options.webview;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import alarm.ruiqi.com.alarm_pro.R;
import alarm.ruiqi.com.alarm_pro.options.MainActivity;
import alarm.ruiqi.com.alarm_pro.service.MobileApplication;
import alarm.ruiqi.com.alarm_pro.utils.Constant;

public class GeneralWebviewFragment extends Fragment implements MainActivity.MainActivityInterInteractionListener{

    private String mUrl;

    public static GeneralWebviewFragment newInstance(String url) {
        GeneralWebviewFragment fragment = new GeneralWebviewFragment();
        Bundle args = new Bundle();
        args.putString(Constant.GENERAL_SAVE_KEY,url);
        fragment.setArguments(args);
        try {
            ((MainActivity) MobileApplication.getInstance().getCtx(MainActivity.class.getSimpleName())).setMainActivityInterInteractionListener(fragment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fragment;
    }

    public GeneralWebviewFragment() {
    }

    public void setUrl(String mUrl) {
        this.mUrl = mUrl;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mUrl = getArguments().getString(Constant.GENERAL_SAVE_KEY);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


       View v =   inflater.inflate(R.layout.fragment_general_webview, container, false);
       findById(v);
       initView();
       return v;
    }

    //========================================
    /**
     * 进度条
     */
    private ProgressBar progressBar;
    /**
     * 工具栏顶部横线
     */
    private View toolbarline;

    /**
     * 是否点击刷新按钮
     */
    private RelativeLayout webviewBottom;
    /**
     * 网页
     */
    private WebView webView;
    private WebSettings settings;


    /**
     * 初始化界面
     */
    private void initView() {
        System.out.println(mUrl);
        webView.loadUrl(mUrl);
    }


    /**
     * 设置监听
     */
    private void setListener() {

    }


    /**
     * 初始化ui
     */
    @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
    private void findById(View v) {

        toolbarline = v.findViewById(R.id.toolbarline);
        webviewBottom = (RelativeLayout) v.findViewById(R.id.webviewBottom);
        progressBar = (ProgressBar) v.findViewById(R.id.progressbar);
        webView = (WebView) v.findViewById(R.id.webview);

        settings = webView.getSettings();
        // 设置WebView属性，能够执行Javascript脚本
        settings.setJavaScriptEnabled(true);
        // js与android调用接口
        webView.addJavascriptInterface(new MyJavascriptInterface(), "android");
        // 设置Web视图
        webView.setWebViewClient(new MyWebViewClient());
        // 处理Javascript的对话框、网站图标、网站title、加载进度等
        webView.setWebChromeClient(new MyWebChromeClient());
    }

    @Override
    public void onDrawerSlide(float slideOffset) {

    }


    class MyJavascriptInterface {
        public MyJavascriptInterface() {
        }

    }


    class MyWebChromeClient extends WebChromeClient {


        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
        }


        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressBar.setProgress(newProgress);
                progressBar.setVisibility(View.GONE);


            } else {
                if (progressBar.getVisibility() == View.GONE)
                    progressBar.setVisibility(View.VISIBLE);
                progressBar.setProgress(newProgress);
            }


            super.onProgressChanged(view, newProgress);
        }
    }


    class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }


    /*@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP && webView.canGoBack()) {
            webView.goBack();
            return true;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }*/




    @Override
    public void onDestroy() {
        super.onDestroy();
        webView.clearHistory();
    }

    public boolean canGoBack(){
        return  webView.canGoBack();
    }


    public void goBack() {
        webView.goBack();
    }

}
